package com.example.testanimation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.testanimation.javabean.User;


public class Addstudent extends AppCompatActivity {
    private com.example.testanimation.MySQLiteOpenHelper1 mySQLiteOpenHelper;
    private EditText meduser,medpassword,mname,mclassName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Compiler EdgeToEdge = null;
        EdgeToEdge.enable();
        setContentView(R.layout.activity_addstudent);
        mySQLiteOpenHelper = new com.example.testanimation.MySQLiteOpenHelper1(this);
        find();

    }
    public void find()
    {
        meduser=findViewById(R.id.ed_username_add);
        medpassword=findViewById(R.id.ed_password_add);
        mname=findViewById(R.id.student_id_add);
        mclassName=findViewById(R.id.student_class_add);
    }
    public void addStudent_(View view) {
        // 获取输入框的值
        String s1 = meduser.getText().toString();
        String s2 = medpassword.getText().toString(); // 注意: 实际开发中密码处理需加密存储
        String s3 = mname.getText().toString();
        String s4 = mclassName.getText().toString();

        // 创建User对象
        User newUser = new User(s1, s2, s3, s4); // 确保User类有此构造方法

        // 构建并显示确认添加的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认添加")
                .setMessage("您确定要添加这位学生吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    // 用户点击确认，执行添加操作
                    long registerResult = mySQLiteOpenHelper.register(newUser);
                    if (registerResult != -1) {
                        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "添加失败，用户名可能已被占用", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", (dialog, which) -> {
                    // 用户点击取消，对话框关闭，不执行添加操作
                    dialog.cancel();
                })
                .create()
                .show();
    }
}